<template>
  <div class="shop-container">
    <div class="header">
      <!-- <el-button type="text"></el-button> -->

      <div>购物车</div>

      <!-- <van-icon name="scan" color="#1989fa" /> -->
    </div>

    <div class="edit-box">
      <van-button
        icon="edit"
        style="border: none"
        size="mini"
        :type="isEdit ? 'danger' : 'default'"
        @click="editClick"
      >
      编辑
      </van-button>
    </div>

    <div class="goods-container">
      <div class="goods-box" v-for="(item, index) in shopList" :key="index">
        <div class="checked-box">
          <van-checkbox v-show="!isEdit" @change="itemCheckedChange" v-model="item.checked"></van-checkbox>
          <van-button v-show="isEdit" @click="deleteGoods(index)" icon="minus" type="danger" size="mini"></van-button>
        </div>

        <div class="img-box">
          <img :src="item.picUrl" style="width: 100%; height: 100%" alt="" />
        </div>

        <div class="info-box">
          <div class="goods-name">{{ item.goodsName }}</div>
          <div class="goods-tags">
            <span v-for="(item, index) in item.specifications" :key="index">{{
              item
            }}</span>
          </div>
          <div class="price-box">
            <span>￥{{ item.price }}</span>
            <van-stepper
              input-width="20px"
              button-size="20px"
              v-model="item.number"
              @change="getTotal"
            />
          </div>
          <div class="date">
            <span>添加日期:{{ item.addTime | filterDate }}</span>
          </div>
        </div>
      </div>
    </div>
<!-- 首屏加载优化 -->
<!-- webpack -->
    <div class="footer-box">
      <div class="all-checked">
        <van-checkbox v-model="allChecked" @change="allCheckedChange">全选</van-checkbox>
      </div>
      <div class="total">
        <span style="font-size: 14px">总计</span>
        <span style="font-size: 16px; color: red">￥{{ total }}</span>
      </div>
      <div class="total-btn">结算({{allCount}})</div>
    </div>

    <!-- {{shopList}} -->
  </div>
</template>

<script>
import request from '../fetch/index.js'
import moment from 'moment'
export default {
  data() {
    return {
      isEdit: false,
      allChecked: false,
      total: 0,
      // allCount: 0,
      shopList: [
        // {
        //   checked: false,
        //   imgUrl: require("../assets/images/apple-1.jpg"),
        //   goodsName: "美的空调1",
        //   goodsTags: ["标准1", "乳白色1"],
        //   goodsPrice: 666.0,
        //   goodsCount: 1,
        //   createDate: "2021-11-25",
        // },
        // {
        //   checked: false,
        //   imgUrl: require("../assets/images/apple-2.jpg"),
        //   goodsName: "美的空调2",
        //   goodsTags: ["标准2", "乳白色2"],
        //   goodsPrice: 777.0,
        //   goodsCount: 1,
        //   createDate: "2021-12-251",
        // },
        // {
        //   checked: false,
        //   imgUrl: require("../assets/images/apple-3.jpg"),
        //   goodsName: "美的空调3",
        //   goodsTags: ["标准3", "乳白色3"],
        //   goodsPrice: 888.0,
        //   goodsCount: 1,
        //   createDate: "2021-13-252",
        // },
        // {
        //   checked: false,
        //   imgUrl: require("../assets/images/apple-4.jpg"),
        //   goodsName: "美的空调4",
        //   goodsTags: ["标准4", "乳白色4"],
        //   goodsPrice: 999.0,
        //   goodsCount: 1,
        //   createDate: "2021-14-253",
        // }
      ]
    };
  },
  mounted () {
    this.getShopCarData()
  },
  computed: {
    allCount () {
      return this.shopList.length
    }
  },
  filters: {
    filterDate (value) {
      let time = new Date(value)

      return moment(time).format('yyyy-MM-DD hh:mm:ss')
    }
  },
	methods: {
    getShopCarData () {
      request({
        url: 'http://192.168.200.251:8888/wx/cart/index',
        method: 'get'
      }).then(res => {
        console.log('购物车数据', res)
        this.shopList = res.data.data.cartList

        this.getTotal()
      })
    },
    editClick () {
      this.isEdit = !this.isEdit
    },
    deleteGoods (index) {
      this.shopList.splice(index, 1)
      console.log(123)
    },
		// 从shopList中去过滤checked为true的对象，拿到数组的length
		itemCheckedChange (value) {
			let filterGoods = this.shopList.filter(item => {
				return item.checked === true
			})

			// this.allCount = filterGoods.length

			this.getTotal()
		},
		// 循环shopList数组，将每个对象的goodsPrice属性加起来
		getTotal () {
			// prev作为第一个数组的元素
			// next作为下一个数组的元素

      let filterGodds = this.shopList.filter(item => {
        return item.checked === true
      })

			// 每次循环过后，prev接收上一次return的值作为，下一次的prev

			this.total = filterGodds.reduce((prev, next) => {
        return prev + (next.price * next.number)
      }, 0)

      console.log(this.total)
		},
		allCheckedChange (value) {
			this.shopList = this.shopList.map(item => {
				return {
					...item,
					checked: value
				}
			})
		}
	}
};
</script>

<style lang="scss" scoped>
.shop-container {
  display: flex;
  flex-direction: column;
  height: 100%;

	.goods-container {
		flex: 1;
		overflow: auto;
	}
}
.header {
  width: 100%;
  height: 50px;
  border-bottom: 1px solid #ddd;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0 10px;
  box-sizing: border-box;
  background: #fff;
}

.edit-box {
  // display: flex;
  // justify-content: flex-end;
  text-align: right;
  padding-right: 10px;
  box-sizing: border-box;
}

.goods-box {
  margin-bottom: 10px;
  display: flex;
  height: 100px;

  .checked-box {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 50px;
    height: 100%;
  }

  .img-box {
    width: 100px;
    height: 100%;
  }

  .info-box {
    display: flex;
    flex-direction: column;
    flex: 1;
    height: 100%;
    padding-left: 10px;

    .goods-name {
      font-size: 14px;
    }

    .goods-tags {
      font-size: 14px;
      color: #aaa;

      span {
        border: 1px solid #ddd;
        margin-right: 10px;
        padding: 2px 4px;
      }
    }

    .price-box {
      display: flex;
      justify-content: space-between;
      padding-right: 20px;
      color: red;
    }

    .date {
      display: flex;
      align-items: flex-end;
      justify-content: flex-end;
      flex: 1;
      font-size: 14px;
      color: #aaa;
      text-align: right;
      padding-right: 20px;
    }
  }
}

.footer-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 50px;
  padding-left: 15px;
  box-sizing: border-box;

  .total-btn {
    width: 110px;
    height: 50px;
    background: red;
    line-height: 50px;
    text-align: center;
    color: #fff;
  }
}
</style>
